package com.example.pixelyear.utils

import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.example.pixelyear.Interface.OnClickListener
import com.example.pixelyear.R
import com.example.pixelyear.utils.dialog.BaseDialog
import com.example.pixelyear.utils.dialog.OnlyTextViewDialog
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.stringText
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.jetbrains.anko.runOnUiThread


object TipsDialogUtils {

    fun showSureCancelDialog(
        context: Context,
        title: String = "提示",
        okText: String = "确定",
        cancelText: String = "取消",
        content: String,
        okListener: () -> Unit
    ) {
        val dialog = MaterialAlertDialogBuilder(context)
        dialog.setTitle(title)
        dialog.setMessage(content)
        dialog.setNegativeButton(okText) { _, _ ->
            okListener.invoke()
        }
        if (cancelText.isNotEmpty())
            dialog.setPositiveButton(cancelText) { _, _ -> }
        dialog.show()
    }

    fun showSureDialog(
        context: Context,
        title: String = "提示",
        okText: String = "确定",
        content: String,
        okListener: () -> Unit
    ) {
        val dialog = MaterialAlertDialogBuilder(context)
        dialog.setTitle(title)
        dialog.setMessage(content)
        dialog.setNegativeButton(okText) { _, _ ->
            okListener.invoke()
        }
        dialog.show()
    }

    fun showEditDialog(
        context: Context,
        title: String = "提示",
        okText: String = "确定",
        cancelText: String = "取消",
        hint: String = "请输入文件密码",
        okListener: (pass: String, dialog: AlertDialog) -> Unit
    ) {
        val builder = MaterialAlertDialogBuilder(context)
        val edit = EditText(context)
        edit.hint = hint
        builder.setTitle(title)
        builder.setView(edit)
        builder.setNegativeButton(okText, null)
        if (cancelText.isNotEmpty())
            builder.setPositiveButton(cancelText) { _, _ -> }
        val dialog = builder.create()
        dialog.show()
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnDoubleClickListener {
            if (edit.stringText().isNotEmpty())
                okListener.invoke(edit.stringText(), dialog)
            else
                context.runOnUiThread {
                    Utils.showToast("输入不能为空")
                }
        }
    }


    fun showBanCancelDialog(
        context: Context,
        title: String = "提示",
        content: String,
        okText: String = "确定",
        cancelText: String = "取消",
        banCancel: Boolean = false,
        finish: () -> Unit
    ) {
        val builder = MaterialAlertDialogBuilder(context)
        builder.setTitle(title)
        builder.setMessage(content)
        builder.setNegativeButton(okText, null)
        if (cancelText.isNotEmpty())
            builder.setPositiveButton(cancelText) { _, _ -> }
        val dialog = builder.create()
        dialog.setCancelable(!banCancel)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnDoubleClickListener {
            if (banCancel) {
                dialog.dismiss()
                finish.invoke()
            } else
                dialog.dismiss()
        }
    }


    //弹出一个只有TextView的对话框
    fun showOnlyTextDialog(context: Context, text: String, click: (dialog: AlertDialog) -> Unit) {
        val dialog = OnlyTextViewDialog(context)
        dialog.show(text)
        dialog.setonClickListener(OnClickListener {
            click.invoke(dialog)
        })
    }

}
